#include "ET++.ph"
#ifdef __GNUG__
#pragma implementation
#endif

#include "FindChange_e.h"

#include "Class.h"
#include "Application.h"
#include "FindDialog.h"
#include "ChangeDialog.h"
#include "TextView.h"
#include "MenuBar.h"
#include "Menu.h"
#include "String.h"

static ChangeDialog *changeDialog= 0;
static FindDialog *findDialog= 0;

void FindChange::ShowFindDialog(TextView *tv)
{
	if (findDialog == 0) {
		findDialog= new FindDialog("Find");
		gApplication->AddManager(findDialog);
	}
	findDialog->SetTextViewFocus(tv);
	findDialog->ShowOnWindow(tv->GetWindow());
}

void FindChange::HideFindDialog(TextView *tv)
{
	if (findDialog == 0)
		return;
	if (tv == 0 || tv == findDialog->GetTextViewFocus())
		findDialog->Hide();
}

void FindChange::ShowChangeDialog(TextView *tv)
{
	if (changeDialog == 0) {
		changeDialog= new ChangeDialog("Find/Change");
		gApplication->AddManager(changeDialog);
	}
	changeDialog->SetTextViewFocus(tv);
	changeDialog->ShowOnWindow(tv->GetWindow());
}

void FindChange::HideChangeDialog(TextView *tv)
{
	if (changeDialog == 0)
		return;
	if ((tv == 0) || (tv == changeDialog->GetTextViewFocus()))
		changeDialog->Hide();
}

void FindChange::FindAgain(TextView *tv)
{
	if (changeDialog != 0) {
		changeDialog->SetTextViewFocus(tv);
		changeDialog->FindAgain();
	}
	else if (findDialog != 0) {
		findDialog->SetTextViewFocus(tv);
		findDialog->FindAgain();
	}
}

void FindChange::DoSetupMenu(Menu *menu)
{
	const char *pattern= "";
	if (changeDialog != 0)
		pattern= changeDialog->GetSearchPattern();
	else if (findDialog != 0)
		pattern= findDialog->GetSearchPattern();
	menu->EnableItem(cFINDAGAIN, strlen(pattern) != 0);
}

void FindChange::InstallFind(MenuBar *mb, TextView *tv)
{
	Menu *m= mb->FindMenu(cEDITMENU);
	if (m)
		m->InsertItemsAfter(cLASTEDIT,
			"Find...@F",  cFIND,
			"Find Again @G", cFINDAGAIN,
			0
		);
	if (tv)
		tv->SetFlag(eTextWantFindFocus);
}

void FindChange::InstallChange(MenuBar *mb, TextView *tv)
{
	Menu *m= mb->FindMenu(cEDITMENU);
	if (m)
		m->InsertItemsAfter(cLASTEDIT,
			"Find/Change...@F",  cFIND,
			"Find Again @G", cFINDAGAIN,
			0
		);
	if (tv)
		tv->SetFlag(eTextWantFindFocus);
}

void FindChange::SetFocus(TextView *tv)
{
	if (tv->TestFlag(eTextWantFindFocus)) {
		if (changeDialog != 0)
			changeDialog->SetTextViewFocus(tv);
		if (findDialog != 0)
			findDialog->SetTextViewFocus(tv);
	}
}

